package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Dto.OrdersDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author huhanzhao
 * @create 2022-11-12-11:47
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page,int pageSize){
        Page<OrdersDto> ordersDtoPage = ordersService.getPage(page, pageSize);
        return R.success(ordersDtoPage);
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize,Long number,String beginTime,String endTime){
        Page<Orders> page1 = ordersService.page(page, pageSize,number,beginTime,endTime);
        return R.success(page1);
    }

    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId,orders.getId());
        updateWrapper.set(Orders::getStatus,orders.getStatus());
        ordersService.update(updateWrapper);
        return R.success("修改成功");
    }
}
